package com.compass.doctor.base

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.inputmethod.InputMethodManager
import androidx.viewbinding.ViewBinding
import com.compass.doctor.ui.login.LoginActivity
import com.compass.framework.base.BaseActivity
import com.compass.framework.constant.LOGIN_FAILURE
import com.compass.framework.ext.saveAs
import com.compass.framework.ext.saveAsUnChecked
import com.compass.framework.manager.ActivityManager
import com.compass.framework.manager.DictionariesManager
import com.compass.framework.manager.UserInfoManager
import com.compass.framework.utils.LiveDataBus
import java.lang.reflect.ParameterizedType

/**
 *ViewBinding+ViewModel基类
 */
abstract class BaseVBActivity<VB : ViewBinding> : BaseActivity() {

    lateinit var mBinding: VB


    override fun setContentLayout() {
        val type = javaClass.genericSuperclass
        val vbClass: Class<VB> = type!!.saveAs<ParameterizedType>().actualTypeArguments[0].saveAs()
        val method = vbClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
        mBinding = method.invoke(this, layoutInflater)!!.saveAsUnChecked()
        setContentView(mBinding.root)
    }


    override fun getLayoutResId(): Int = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        registerAccountObservable()
    }

    /**
     * 添加账号异常监听
     */
    private fun registerAccountObservable() {
        LiveDataBus.get().with(LOGIN_FAILURE, String::class.java).observe(this) {
            UserInfoManager.clearUserInfo()
            DictionariesManager.clearInfo()
            //这里增加判断，如果栈顶的页面已经是登录页面，则不需要跳转
            if(ActivityManager.top() is LoginActivity){
            }else{
                LoginActivity.start(this)
                ActivityManager.finishOtherActivity(LoginActivity::class.java)
            }
        }
    }

//    @SuppressLint("ServiceCast")
//    fun getTopActivity(context: Context):Activity{
//        val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as android.app.ActivityManager
//        // getRunningTasks()方法在API 21以后就被弃用了，不推荐使用
//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
//            val taskInfo = manager.appTasks
//            return taskInfo[0].taskInfo.topActivity
//        } else {
//            // 使用getRunningTasks()方法获取栈顶Activity
//            val taskInfo = manager.getRunningTasks(1)
//            return taskInfo[0].topActivity
//        }
//
//    }

    private val imm: InputMethodManager? by lazy { getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager? }

    /**
     * 隐藏键盘
     */
    fun hideSoftInput() {
        this?.currentFocus?.let {
            imm?.hideSoftInputFromWindow(it.windowToken, 0)
        }
    }
    /**
     * 显示键盘
     */
    fun showSoftInput() {
        this?.currentFocus?.let {
            imm?.showSoftInput(it, 0)
        }
    }
}